#!/usr/bin/env ruby
# this script builds symbols.js (required for the symbolinfo.js test)
# from the internal symbol table in libnanomsg.
#
# Regenerate the symbols each time a new major release of libnanomsg
# is incorporated, with:
#
# ./mksymbols > symbols.js
#
src = '../deps/nanomsg/src/core/symbol.c'
opts = []

s = File.open(src) { |f| f.read }

if m = s.match(/static\s+const\s+struct\s+nn_symbol_properties\s+
                sym_value_names\s+\[\]\s+=\s+{\s+
                (.*?)\s+};/mx)

    m[1].scan(/NN_SYM\((\w+),\s*(\w+),\s*(\w+),\s*(\w+)\),?/) do |n|
      opts.push(<<-FOO)
                'value': nn.#{n[0]},
                'name':  "#{n[0].strip}",
                'ns':    nn.NN_NS_#{n[1]},
                'type':  nn.NN_TYPE_#{n[2].strip},
                'unit':  nn.NN_UNIT_#{n[3]}
      FOO
    end
end

o = opts.map { |t| "{\n" + t + "}" }.join(",\n")

puts <<FOO
// autogenerated from #{src} - manual edits inadvisable!
var nano = require('../');
var nn = nano._bindings;

exports.symbols = [
#{o}
];
FOO

